package com.example.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.security.annotation.AuthMethod;
import com.example.core.service.BaseService;
import com.example.core.utils.Result;
import com.example.core.utils.StringUtils;
import com.example.core.vo.BaseVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

public class BaseController<S extends BaseService, V extends BaseVo> {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private S service;

    @AuthMethod(expression = "AddRoleFunction()")
    @PostMapping(value = "save")
    public Result save(@RequestBody V vo) {
        try {
            String id = this.service.add(vo);
            return Result.success(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @AuthMethod(expression = "EditRoleFunction() || EditorFunction()")
    @PostMapping(value = "edit")
    public Result edit(@RequestBody V vo) {
        try {
            String id = this.service.update(vo);
            return Result.success(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @AuthMethod(expression = "ReadRoleFunction() || ReaderFunction()")
    @GetMapping(value = "view")
    public Result view(@RequestParam String id) {
        try {
            Object vo = this.service.view(id);
            return Result.success(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @AuthMethod(expression = "DeleteRoleFunction() || DeleteFunction()")
    @DeleteMapping(value = "delete")
    public Result delete(@RequestParam String id) {
        try {
            this.service.delete(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @AuthMethod(expression = "DefaultRoleFunction()")
    @PostMapping(value = "getPage")
    public Result getPage(@RequestBody V vo) {
        try {
            QueryWrapper queryWrapper = changeFindPageQueryWrapper(vo);
            IPage pageRtn = this.service.findPage(vo, queryWrapper);
            return Result.success(pageRtn);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    public QueryWrapper changeFindPageQueryWrapper(V vo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(vo.getName())) {
            queryWrapper.like("name", vo.getName());
        }
        queryWrapper.orderByDesc("update_time");
        return queryWrapper;
    }

    public S getService() {
        return service;
    }
}
